<template>
  <view class="cart">
    <view
      class="goods-list"
      v-for="(item, index) in cartList"
      :key="item.goods_id"
    >
      <view class="goods">
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <radio-group @click="checkedFn(index)">
            <radio class="chk" color="#eb4450" :checked="item.checked"></radio>
          </radio-group>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image
            width="100%"
            mode="widthFix"
            :src="item.goods_small_logo"
          ></u-image>
        </view>
        <!-- 3 名称 -->
        <view class="goods-info">
          <!-- 商品名称 -->
          <view class="goods-name u-line-2">{{ item.goods_name }}</view>
          <!-- 商品编辑栏 -->
          <view class="goods-tool">
            <view class="goods-price">￥{{ item.goods_price }}</view>
            <view class="goods-count">
              <view class="goods-edit" @click="handleUpdate(index, -1)">-</view>
              <view class="goods-num">{{ item.count }}</view>
              <view class="goods-edit" @click="handleUpdate(index, 1)">+</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 底部操作栏 -->
    <view class="floot">
      <view class="allChecked">
        <radio-group @click="handleAll">
          <radio color="#eb4450" :checked="allChecked">全选</radio>
        </radio-group>
      </view>
      <view class="heji"> 合计：¥{{ allPrice }}</view>
      <view class="close" @click="goPay">去结算({{ allSum }})</view>
    </view>
  </view>
</template>

<script>
export default {
  computed: {
    //数据从vuex 仓库拿
    cartList() {
      return this.$store.state.cartList;
    },
    //小选控制全选
    allChecked() {
      return (
        this.cartList.length && this.cartList.every((goods) => goods.checked)
      );
    },
    //合计总价
    allPrice() {
      //用getters改造数据

      return this.$store.getters.total;
    },
    //总件数
    allSum() {
      //用getters改造数据
      return this.$store.getters.doneTodos; //总件数
    },
  },

  methods: {
    //点击是否选中状态
    checkedFn(index) {
      const cartList = this.$store.state.cartList;
      cartList[index].checked = !cartList[index].checked;
      this.$store.commit("cartListNum", cartList);
    },

    //点击加减 数量变化
    async handleUpdate(index, num) {
      const cartList = this.$store.state.cartList;

      if (cartList[index].count == 1 && num == -1) {
        const [err, result] = await uni.showModal({
          title: "提示",
          content: "你舍得删除吗！",
          showCancel: true,
          cancelText: "取消",
          cancelColor: "#eb4450",
          confirmText: "确定",
          confirmColor: "#eb4450",
        });

        if (result.confirm) {
          //  点击确定就删除
          cartList.splice(index, 1);
          this.$store.commit("cartListNum", cartList);
        } else {
          // 取消不做操作
        }
      } else {
        cartList[index].count += num;
        this.$store.commit("cartListNum", cartList);
      }
    },

    //点击全选 控制小选
    handleAll() {
      //拿到数据
      const cartList = this.$store.state.cartList;
      const allChecked = !this.allChecked;
      //修改数据
      cartList.forEach((v) => {
        v.checked = allChecked;
      });
      //发送给vuex仓库
      this.$store.commit("cartListNum", cartList);
    },
    //点击跳转到支付页面
    goPay() {
      //先判断有没有选中的商品 没有则提示 有再跳
      if (this.allSum > 0) {
        //跳转到支付页面
        uni.navigateTo({
          url: "/pages/pay/pay",
        });
      } else {
        uni.showToast({
          title: "您还没有选中的商品！",
          icon: "none",
        });
      }
    },
  },
};
</script>

<style lang="scss">
.cart {
  .goods-list {
    .goods {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #eee;
      .goods-chk {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        .chk {
          border-radius: 50%;
        }
      }
      .goods-img {
        flex: 2;
        padding: 10rpx;
      }
      .goods-info {
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }
        .goods-tool {
          display: flex;
          justify-content: space-between;
          .goods-price {
            font-size: 30rpx;
            color: $themeColor;
          }

          .goods-count {
            width: 150rpx;
            justify-content: space-between;
            display: flex;
            .goods-edit {
              width: 40rpx;
              height: 40rpx;
              border: 1px solid #ccc;
              display: flex;
              align-items: center;
              justify-content: center;
              border-radius: 50%;
            }

            .goods-num {
            }
          }
        }
      }
    }
  }
  .floot {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 83rpx;
    border-top: 1rpx solid #eee;
    display: flex;
    align-items: center;
    padding: 0 15rpx;
    background-color: #fff;
    .allChecked {
      flex: 1;
    }
    .heji {
      flex: 3;
      color: $themeColor;
    }
    .close {
      flex: 1;
      background-color: $themeColor;
      text-align: center;
      line-height: 52rpx;
      color: #fff;
      border-radius: 26rpx;
      width: 150rpx;
      height: 52rpx;
    }
  }
}
</style>